package com.nanbei.a02;

import org.springframework.beans.factory.support.DefaultListableBeanFactory;
import org.springframework.beans.factory.xml.XmlBeanDefinitionReader;
import org.springframework.boot.autoconfigure.web.servlet.DispatcherServletRegistrationBean;
import org.springframework.boot.web.embedded.tomcat.TomcatServletWebServerFactory;
import org.springframework.boot.web.servlet.context.AnnotationConfigServletWebServerApplicationContext;
import org.springframework.boot.web.servlet.server.ServletWebServerFactory;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.context.support.FileSystemXmlApplicationContext;
import org.springframework.core.io.ClassPathResource;
import org.springframework.web.servlet.DispatcherServlet;
import org.springframework.web.servlet.mvc.Controller;

/**
 * @ClassDescription:
 * @JdkVersion: 1.8
 * @Author: libs
 * @Created: 2024/9/10 14:21
 */
public class A02Application {

    public static void main(String[] args) {

        //testClassPathXmlApplicationContext();
        //testFileSystemXmlApplicationContext();
        //testAnnotationConfigApplicationContext();
        testAnnotationConfigServletWebServerApplicationContext();

        /*DefaultListableBeanFactory beanFactory = new DefaultListableBeanFactory();
        System.out.println("读取xml文件之前的bean");
        for (String beanDefinitionName : beanFactory.getBeanDefinitionNames()) {
            System.out.println(beanDefinitionName);
        }
        XmlBeanDefinitionReader xmlBeanDefinitionReader = new XmlBeanDefinitionReader(beanFactory);
       // xmlBeanDefinitionReader.loadBeanDefinitions(new FileSystemResource("src\\main\\resources\\b01.xml"));
        xmlBeanDefinitionReader.loadBeanDefinitions(new ClassPathResource("b01.xml"));
        System.out.println("读取xml文件之后的bean");
        for (String beanDefinitionName : beanFactory.getBeanDefinitionNames()) {
            System.out.println(beanDefinitionName);
        }*/


    }


    public static void testClassPathXmlApplicationContext(){
        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("b01.xml");
        String[] beanDefinitionNames = context.getBeanDefinitionNames();
        for (String beanDefinitionName : beanDefinitionNames) {
            System.out.println(beanDefinitionName);
        }

        System.out.println("------------------------");
        System.out.println(context.getBean(A02Bean2.class).getA02Bean1());

    }

    public static void testFileSystemXmlApplicationContext(){
        FileSystemXmlApplicationContext context =
                new FileSystemXmlApplicationContext("src/main/resources/b01.xml");
        for (String beanDefinitionName : context.getBeanDefinitionNames()) {
            System.out.println(beanDefinitionName);
        }

        System.out.println("------------------------");
        System.out.println(context.getBean(A02Bean2.class).getA02Bean1());

    }

    public static void testAnnotationConfigApplicationContext(){
        AnnotationConfigApplicationContext context =
                new AnnotationConfigApplicationContext(A02Config.class);
        for (String beanDefinitionName : context.getBeanDefinitionNames()) {
            System.out.println(beanDefinitionName);
        }
        System.out.println(context.getBean(A02Bean2.class).getA02Bean1());
    }

    public static void testAnnotationConfigServletWebServerApplicationContext(){
        AnnotationConfigServletWebServerApplicationContext context =
                new AnnotationConfigServletWebServerApplicationContext(WebA02Config.class);
        for (String beanDefinitionName : context.getBeanDefinitionNames()) {
            System.out.println(">>>>"+beanDefinitionName);
        }

    }

    @Configuration
    static class WebA02Config{

        @Bean
        public ServletWebServerFactory servletWebServerFactory(){
            return new TomcatServletWebServerFactory();
        }

        @Bean
        public DispatcherServlet dispatcherServlet(){
            return new DispatcherServlet();
        }

        @Bean
        public DispatcherServletRegistrationBean dispatcherServletRegistrationBean(){
            return new DispatcherServletRegistrationBean(dispatcherServlet(),"/");
        }

        @Bean("/hello")
        public Controller controller(){
            return (httpServletRequest, httpServletResponse) -> {
                httpServletResponse.getWriter().print("hello");
                return null;
            };
        }

    }


    @Configuration
    public static class A02Config{

        @Bean
        public A02Bean1 a02Bean1(){
            return new A02Bean1();
        }

        @Bean
        public A02Bean2 a02Bean2(A02Bean1 a02Bean1){
            A02Bean2 a02Bean2 = new A02Bean2();
            a02Bean2.setBean01(a02Bean1);
            return a02Bean2;
        }
    }



    static class A02Bean1{

    }

    static class A02Bean2{
        private A02Bean1 a02Bean1;

        public A02Bean1 getA02Bean1(){
            return this.a02Bean1;
        }

        public void setBean01(A02Bean1 a02Bean1) {
            this.a02Bean1 = a02Bean1;
        }
    }
}
